﻿/******************************************************************************
 * Project: Processor Affinity Manager                                        *
 * File:    Testing.cs                                                        *
 * Author:  Aaron Jones                                                       *
 * Version: 1                                                                 *
 *                                                                            *
 * Description:                                                               *
 *    This file defines various tests created to test the functionality of    *
 *    modules.                                                                *
 ******************************************************************************/

using Microsoft.Win32;
using System;

namespace ProcessorAffinityManager
{
    class Testing
    {
        static void Main(string[] args)
        {
            ManagedRegistryKeyTest();
            ConfigurationFileTest();
            ShellExtensionTest();

            Console.WriteLine("TESTING PASSED");
            Console.ReadKey();
        }

        static void ManagedRegistryKeyTest()
        {
            bool threw;

            // Constructor 1
            ManagedRegistryKey key = new ManagedRegistryKey(
                Registry.LocalMachine, @"SOFTWARE\ProcessorAffinityManagerTEST");
            if (key.Exists()) throw new Exception("Don't start the test with the key existing");

            // SetValue 1 and GetValue 1
            key.SetValue("TestValue1");
            if ((string)key.GetValue().Value != "TestValue1" ||
                key.GetValue().ValueKind != RegistryValueKind.String)
                throw new Exception("Unexpected value");

            // SetValue 2 and GetValue 2
            key.SetValue(null, "TestValue2");
            if ((string)key.GetValue(null).Value != "TestValue2" ||
                key.GetValue(null).ValueKind != RegistryValueKind.String)
                throw new Exception("Unexpected value");

            // SetValue 3 and GetValue 3
            key.SetValue(null, "TestValue3", RegistryValueKind.String);
            if ((string)key.GetValue(null).Value != "TestValue3" ||
                key.GetValue(null).ValueKind != RegistryValueKind.String)
                throw new Exception("Unexpected value");

            // SetValue 2 with invalid name.
            threw = false;
            try
            {
                key.SetValue("TestValue4", "ShouldThrow");
            }
            catch
            {
                threw = true;
            }
            if (!threw)
                throw new Exception("Didn't throw when needed!");

            // GetValue 2 with invalid name.
            threw = false;
            try
            {
                key.GetValue("TestValue5");
            }
            catch
            {
                threw = true;
            }
            if (!threw)
                throw new Exception("Didn't throw when needed!");

            key.Delete();
            if (key.Exists())
                throw new Exception("Key exists after deletion!");
        }

        static void ConfigurationFileTest()
        {
            int entryCount = 2000;
            ConfigurationFile myFile = new ConfigurationFile();

            // Start with a clean file.
            myFile.Clear();
            if(myFile.Defaults.Count != 0) throw new Exception("Count nonzero!");

            // Adding entries.
            for (int i = 1; i <= entryCount; i++)
            {
                myFile.AddDefault(new Default(i.ToString(), i));
                if (myFile.Defaults.Count != i) throw new Exception("Count not accurate!");
            }

            // Adding existing entries.
            for (int i = 1; i <= entryCount; i++)
            {
                myFile.AddDefault(new Default(i.ToString(), i));
                if (myFile.Defaults.Count != entryCount) throw new Exception("Count not accurate!");
            }

            // Removing entries.
            for (int i = 1; i <= entryCount; i++)
            {
                myFile.RemoveDefault(myFile.Defaults[0]);
                if (myFile.Defaults.Count != entryCount - i) throw new Exception("Count not accurate!");
            }

            // Add entries, reload, and check counts.
            for (int i = 1; i <= entryCount; i++)
            {
                myFile.AddDefault(new Default(i.ToString(), i));
                if (myFile.Defaults.Count != i) throw new Exception("Count not accurate!");
            }

            if (myFile.Defaults.Count != entryCount) throw new Exception("Count not accurate!");

            // End with a clean file.
            myFile.Clear();
            if (myFile.Defaults.Count != 0) throw new Exception("Count not accurate!");
        }

        static void ShellExtensionTest()
        {
            int repeatCount = 10;

            // Repeatedly call the Install and Remove functions.
            for (int i = 0; i < repeatCount; i++)
                ShellExtension.Remove();
            for (int i = 0; i < repeatCount; i++)
                ShellExtension.Install();
            for (int i = 0; i < repeatCount; i++)
            {
                ShellExtension.Remove();
                ShellExtension.Install();
            }

            // Make sure the exists flag is working.
            if (!ShellExtension.Exists)
                throw new Exception("Shell Extension Does Not Exist");
            ShellExtension.Remove();
            if (ShellExtension.Exists)
                throw new Exception("Shell Extension Exists");
        }
    }
}
